package com.sany.taskmanager.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.sany.taskmanager.R;
import com.sany.taskmanager.bean.FactoryBean;
import com.sany.taskmanager.databinding.ActivityLoginBinding;
import com.sany.taskmanager.global.Key;
import com.sany.taskmanager.http.IHttpCallBack;
import com.sany.taskmanager.manager.DataManager;
import com.sany.taskmanager.ui.widget.FactoryDialog;
import com.sany.taskmanager.util.ClickUtil;
import com.sany.taskmanager.util.LogUtil;
import com.sany.taskmanager.util.PreferenceUtil;
import com.sany.taskmanager.util.ToastUtil;

/**
 * 登录页面
 */
public class LoginActivity extends BaseActivity<ActivityLoginBinding> implements View.OnClickListener {

    private boolean isSaveUsername;
    private FactoryBean mFactoryBean;

    @Override
    protected void initData() {
        // 是否保存用户名
        isSaveUsername = PreferenceUtil.getPreferences().getBoolean(Key.SharedKey.IS_SAVE_USERNAME);
        if (isSaveUsername) {
            String username = PreferenceUtil.getPreferences().getString(Key.SharedKey.USERNAME);
            vBinding.etUsername.setText(username);
            vBinding.ivSaveUser.setImageLevel(1);
        } else {
            vBinding.ivSaveUser.setImageLevel(0);
        }
        vBinding.ivSaveUser.setOnClickListener(this);
        vBinding.tvSaveUser.setOnClickListener(this);
        vBinding.tvLogin.setOnClickListener(this);
        vBinding.ivIcon.setOnClickListener(this);
        vBinding.rlFactory.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_save_user || id == R.id.tv_save_user) { // 是否保存用户名
            isSaveUsername = !isSaveUsername;
            if (isSaveUsername) {
                vBinding.ivSaveUser.setImageLevel(1);
            } else {
                vBinding.ivSaveUser.setImageLevel(0);
            }
        } else if (id == R.id.tv_login) { // 登录
            login();
        } else if (id == R.id.iv_icon) {
            ClickUtil.onClick(this);
        } else if (id == R.id.rl_factory) {
            showFactoryDialog();
        }
    }

    private FactoryDialog factoryDialog;

    /**
     * 厂区选择弹框
     */
    private void showFactoryDialog() {
        if (factoryDialog == null) {
            factoryDialog = new FactoryDialog(this);
            factoryDialog.setOnFactoryListener(factoryBean -> {
                mFactoryBean = factoryBean;
                vBinding.tvFactory.setText(mFactoryBean.getFactoryName());
            });
        }
        if (!factoryDialog.isShowing()) {
            factoryDialog.show();
        }
    }

    /**
     * 登录前，字段校验
     */
    private void login() {
        String username = vBinding.etUsername.getText().toString().replaceAll(" ", "");
        if (TextUtils.isEmpty(username)) {
            ToastUtil.showToast(this, getString(R.string.tip_username_empty));
            return;
        }
        String password = vBinding.etPassword.getText().toString().replaceAll(" ", "");
        if (TextUtils.isEmpty(password)) {
            ToastUtil.showToast(this, getString(R.string.tip_password_empty));
            return;
        }
        if (mFactoryBean == null) {
            ToastUtil.showToast(this, getString(R.string.tip_factory_empty));
            return;
        }
        showLoading();
        compositeDisposable.add(taskViewModel.login(username, password, mFactoryBean.getFactoryCode(), new IHttpCallBack<String>() {
            @Override
            public void onSuccess(String token) {
                hideLoading();
                LogUtil.d("token = " + token);
                if (!TextUtils.isEmpty(token)) {
                    PreferenceUtil.getPreferences().putString(Key.SharedKey.TOKEN, token);
                }
                PreferenceUtil.getPreferences().putBoolean(Key.SharedKey.IS_LOGIN, true);
                PreferenceUtil.getPreferences().putBoolean(Key.SharedKey.IS_SAVE_USERNAME, isSaveUsername);
                PreferenceUtil.getPreferences().putString(Key.SharedKey.USERNAME, username);
                PreferenceUtil.getPreferences().putString(Key.SharedKey.FACTORY_CODE, mFactoryBean.getFactoryCode());
                DataManager.getInstance().setFactoryCode(mFactoryBean.getFactoryCode());
                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                finish();
            }

            @Override
            public void onFailed(String message) {
                hideLoading();
                ToastUtil.showToast(LoginActivity.this, message);
            }
        }));
    }

    @Override
    protected boolean showUsername() {
        return false;
    }

}
